package com.hcrazy.manchester.app.jdbc;

import java.util.List;

import com.hcrazy.manchester.app.pojo.LiveSort;

public class LiveSortDAO extends BaseDAO<LiveSort>{

	public void addLiveSort(LiveSort sort) throws Exception{
		StringBuffer sb = new StringBuffer();
		sb.append("insert into him_live_sort(");
		sb.append("sortName,sortNum,sortPic,sortParent,funCode,sortDesc,type,sortCode");
		sb.append(") values (");
		sb.append(":sortName,:sortNum,:sortPic,:sortParent,:funCode,:sortDesc,:type,:sortCode");
		sb.append(")");
		
		update(sb.toString(), sort);
	}
	
	public void updateLiveSort(LiveSort sort) throws Exception{
		StringBuffer sb = new StringBuffer();
		sb.append("update him_live_sort set ");
		sb.append(" sortName=:sortName,sortNum=:sortNum,sortPic=:sortPic,sortParent=:sortParent,funCode=:funCode,sortDesc=:sortDesc");
		sb.append(" where sortId = :sortId");
		
		update(sb.toString(), sort);
	}
	
	public List<LiveSort> listAllSorts(int type) throws Exception{
		String sql = "select * from him_live_sort where type = ? order by sortNum";
		return getList(sql, LiveSort.class,type);
	}
	
	public LiveSort getById(int sortId) throws Exception{
		String sql = "select * from him_live_sort where sortId = ?";
		return this.getJavaBean(sql, LiveSort.class, sortId);
	}
	
	public List<LiveSort> getByParentId(int parentId) throws Exception{
		String sql = "select * from him_live_sort where sortParent = ?";
		return getList(sql, LiveSort.class,parentId);
	}
	
	public void delLiveSort(int sortId) throws Exception{
		String sql = "delete from him_live_sort where sortId = ?";
		this.commonUpdate(sql, sortId);
	}
	
	public void delLiveSortByParentId(int parentId) throws Exception{
		String sql = "delete from him_live_sort where sortParent = ?";
		this.commonUpdate(sql, parentId);
	}
	
	public void delLiveSortWithoutParent() throws Exception{
		
	}
	
	public List<LiveSort> getSortByFunCode(int funCode) throws Exception{
		String sql = "select * from him_live_sort where funCode = ? and type = 0";
		return this.getList(sql, LiveSort.class, funCode);
	}
	
	public boolean hasChildren(int parentId) throws Exception{
		String sql = "select count(*) from him_live_sort where sortParent = ?";
		int count = getCount(sql, parentId);

		return count > 0;
	}
}
